import math
import sympy
class police_craft():
    def __init__(self, x, y, index):
        self.x = x
        self.y = y
        self.speed = 45
        self.desx = 0
        self.desy = 0
        self.remain = 0
        self.index = index

    def chase_time(self, ship):
        """
        追逐商船的时间
        :param ship:
        :return: time
        """
        t = sympy.symbols("t")
        ti = sympy.solve((ship.y-t*ship.speed*ship.ssin - self.y) ** 2 + (ship.x - t*ship.speed*ship.ccos-self.x) ** 2 - 2025 * t * t, t)
        for i in ti:
            if i < 0:
                ti.remove(i)
        return min(ti)

    def chase_circle(self, ship):
        """
        追击冲线船
        :param ship:
        :return:
        """
        trace_tangle = math.atan(ship.y / ship.x)
        total_time = 0
        sta = 0
        while ship.x + ship.y < 181.09673245:
            tangle_a = math.atan((ship.y-133.4100407) / (ship.x - 48.55728378))
            tangle_b = math.atan((ship.y-48.55728378) / (ship.x - 133.4100407))
            if tangle_a < trace_tangle - (math.pi/6) and trace_tangle + (math.pi/6) <tangle_b:
                if self.y > math.tan(trace_tangle) * (self.x - ship.x) + ship.y:
                    trace_tangle += math.pi/6
                else:
                    trace_tangle -= math.pi / 6
            else:
                if self.y > math.tan(trace_tangle) * (self.x - ship.x) + ship.y:
                    trace_tangle = tangle_b
                else:
                    trace_tangle = tangle_a
            t = sympy.symbols("t")
            ti = sympy.solve((ship.y - t * 35 * math.sin(trace_tangle) - self.y) ** 2 + (ship.x - t * 35 * math.cos(trace_tangle) - self.x) ** 2 - 2025 * t * t,t)  # 要变换三角函数的数值和商船的速度变为了35km/h
            for i in ti:
                if i < 0:
                    ti.remove(i)
            c_time = min(ti)
            self.chase_angle = math.tan((ship.y - 35*c_time*math.sin(trace_tangle)-self.y)/(ship.x - 35*c_time*math.cos(trace_tangle)-self.x))
            if c_time > 10:
                self.x += 45 / 6 * math.cos(self.chase_angle)
                self.y += 45 / 6 * math.sin(self.chase_angle)
                # 更新10分钟后ship.position
                ship.x -= (35 / 6 * math.cos(trace_tangle))
                ship.y -= (35 / 6 * math.sin(trace_tangle))
                total_time += 1 / 6  # 追捕时间加10分钟
            else:
                total_time += c_time  # 加的时间为追捕时间
                # 更新chase_time_total分钟后police_ship.position
                self.x += 45 * c_time * math.cos(self.chase_angle)
                self.y += 45 * c_time * math.sin(self.chase_angle)
                # 更新chase_time_total分钟后ship.position
                ship.x -= (35 * c_time * math.cos(trace_tangle))
                ship.y -= (35 * c_time * math.sin(trace_tangle))
                sta = 1
                return [total_time, (ship.x, ship.y)]
        if sta == 0:
            return None




